//
//  SubCViewController.swift
//  xiaoqiao
//
//  Created by 冷子豪 on 2017/8/19.
//  Copyright © 2017年 冷子豪. All rights reserved.
//

import UIKit

class SubCViewController: UITableViewController {
    
    var subUserList = [User]()
    var userid:Int?
    
    /// 完成回调属性
    var completionCallBack:(()->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        
        loadData { (list) in
            // ‘拼接’数组,闭包中定义好的代码在需要的时候执行，需要self，指定语境
            self.subUserList += list
            
            // 刷新表格
            self.tableView.reloadData()
        }
    }
    
    /// 利用闭包回调
    private func loadData(completion:@escaping ([User]) -> ()) -> () {
        
        DispatchQueue.global().async {
            print("加载C级下线信息中……")
            
            Thread.sleep(forTimeInterval: 0.1)
            
            // 同步Get方式
            
            var arrayM = [User]()
            let cUser = userViewModel.getUserByUserId(userid: self.userid!)
            arrayM.append(cUser)
            
            // 主线程回调
            DispatchQueue.main.async(execute: {
                
                // 回调，执行闭包
                completion(arrayM + arrayM + arrayM + arrayM + arrayM)
            })
        }
    }
    
    //    // MARK: - 代理方法
    //    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //        tableView.deselectRow(at: indexPath, animated: true)
    //        //        houseTable.deselectRow(at: indexPath, animated: true)
    //
    //        // 执行 segue
    //        performSegue(withIdentifier: "house2detail", sender: indexPath)
    //    }
    
    // MARK: - 数据源方法
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return subUserList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "subid", for: indexPath)
        
        // 1
        let dateLabel:UILabel = cell.viewWithTag(1) as! UILabel
        let nowDate = Date()
        let timeZone = TimeZone.init(identifier: "UTC")
        let formatter = DateFormatter()
        formatter.timeZone = timeZone
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = "yyyy/MM/dd"
        dateLabel.text = formatter.string(from: nowDate)
        
        // 2
        let nameLabel:UILabel = cell.viewWithTag(2) as! UILabel
        nameLabel.text = "姓名：\(String(describing: subUserList[indexPath.row].realname))"
        
        // 3
        let productLabel:UILabel = cell.viewWithTag(3) as! UILabel
        productLabel.text = "产品：安居贷"
        
        
        // 4
        let label4:UILabel = cell.viewWithTag(2) as! UILabel
            label4.text = "审批金额：\(Int(arc4random_uniform(30))+1)万"
        
        
        // 4
        let incomeLabel:UILabel = cell.viewWithTag(4) as! UILabel
        incomeLabel.text = "收益：\(Int(arc4random_uniform(5000))+1001)元"
        
        return cell
        
    }
}
